﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TenderSampleApp.Models;

namespace TenderSampleApp.Controllers
{
    public class AjaxQueryController : Controller
    {
        DAL dl = new DAL();
        // GET: /AjaxQuery/

        #region Action for Ajax
        public String GetFirmName(string code)
        {
            string firmname = dl.GetVatFrimInfo().Where(f => f.Code == code).Select(f => f.Name).FirstOrDefault();
            return String.IsNullOrEmpty(firmname) ? "пусто" : firmname;
        }

        public String GetFirmAddress(string code)
        {
            VAT VatFirm = dl.GetVatFrimInfo().Where(f => f.Code == code).FirstOrDefault();
            if (VatFirm == null)
                return "пусто";
            TenderDBEntities entitty = new TenderDBEntities();
            string fulladdress = (String.IsNullOrEmpty(VatFirm.PostIndex)) ? "" : VatFirm.PostIndex + ", ";

            Street street = entitty.Streets.Where(r => r.ID == VatFirm.StreetID).FirstOrDefault();
            string strstreet = street.Name;
            string strcity = street.City.Name;
            string strdistrict = street.City.District.Name;
            string strregion = street.City.District.Region.Name;
            fulladdress += strregion + " обл.";
            fulladdress += ", район " + strdistrict;
            fulladdress += ", м. " + strcity;
            fulladdress += (strstreet == "-") ? "" : ", вул. " + strstreet;
            fulladdress += (String.IsNullOrEmpty(VatFirm.Build)) ? "" : " " + VatFirm.Build;
            fulladdress += (String.IsNullOrEmpty(VatFirm.Apartment.ToString())) ? "" : ", кв. " + VatFirm.Apartment;
            return fulladdress;
        }
        public JsonResult SearchRegions(string term)
        {
            List<DAL.JSON_Region> res = dl.GetRegions(term.ToLower());
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        public JsonResult SearchJuristicPerson(string code)
        {
            DAL.JSON_Jurist res = dl.GetJuristicPerson(code.ToLower());
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        public JsonResult SearchDistricts(string term, int regionID)
        {
            List<DAL.JSON_District> res = dl.GetDistricts(term.ToLower(), regionID);
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        public JsonResult SearchCities(string term, int districtID)
        {
            List<DAL.JSON_City> res = dl.GetCities(term.ToLower(), districtID);
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        public JsonResult SearchStreets(string term, int cityID)
        {
            List<DAL.JSON_Street> res = dl.GetStreets(term.ToLower(), cityID);
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        public JsonResult SearchPhisicalPersonLastName(string term)
        {
            List<string> res = dl.GetPhysicalPersonLastName(term.ToLower());
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        #endregion
        public JsonResult SearchPhisicalPersonName(string term, string lastname)
        {
            List<string> res = dl.GetPhysicalPersonName(term.ToLower(), lastname.ToLower());
            return Json(res, JsonRequestBehavior.AllowGet);
        }
        public JsonResult SearchPhisicalSurname(string term, string lastname, string name)
        {
           List<DAL.JSON_Person> per = dl.GetPhysicalPersonSurname(term.ToLower(), lastname.ToLower(), name.ToLower());
           return Json(per, JsonRequestBehavior.AllowGet);
        }

        public JsonResult SearchAuction(string number)
        {
            DAL.JSON_Auction auc = dl.GetAuction(number);
            return Json(auc, JsonRequestBehavior.AllowGet);
        }
    }
}
